const ipUtil = require("../../utils/hostUtil")
const requestUtil = require("../../utils/requestUtil")
Page({
    data: {
        waitShipEnable: true,
        alreadyShipEnable: false
    },
    onLoad: function (options) {
        this.loadPage();
    },
    shipEnable: function (e) {
        let state = e.currentTarget.dataset.state;
        if (state == 0) {
            this.setData({
                waitShipEnable: !this.data.waitShipEnable
            })
        } else {
            this.setData({
                alreadyShipEnable: !this.data.alreadyShipEnable
            })
        }
    }
    , enableType: function (e) {
        let index = e.currentTarget.dataset.index;
        let state = e.currentTarget.dataset.state;
        if (state == 0) {
            let waitShipArr = this.data.waitShipArr;
            waitShipArr[index] = !waitShipArr[index];
            this.setData({
                waitShipArr: waitShipArr
            })
        } else {
            let alreadyShipArr = this.data.alreadyShipArr;
            alreadyShipArr[index] = !alreadyShipArr[index];
            this.setData({
                alreadyShipArr: alreadyShipArr
            })
        }

    },
    loadPage: function () {
        var that = this;
        wx.request({
            url: `${ipUtil.host}${requestUtil.all_ship}`,
            method: "POST",
            success(res) {
                let data = res.data.data;
                let waitShipArr = [];
                let alreadyShipArr = [];
                let waitShip = data['1'];
                let alreadyShip = data['2'];
                waitShip.forEach(item => {
                    waitShipArr.push(false);
                })
                alreadyShip.forEach(item => {
                    alreadyShipArr.push(false)
                })
                that.setData({
                    waitShipArr: waitShipArr,
                    alreadyShipArr: alreadyShipArr,
                    waitShip: waitShip,
                    alreadyShip: alreadyShip
                })
            }
        })
    },
    ship:function (e){
        let that = this;
        let index = e.currentTarget.dataset.index;
        let id = e.currentTarget.dataset.id;
        let state = e.currentTarget.dataset.state;
        let expressNo = e.detail.value.expressNo;
        if (expressNo == "" || expressNo == undefined) {
            wx.showToast({
                title: "请输入快递单号",
                icon: "none",
                duration: 2000
            })
            return;
        }
        wx.request({
            url: `${ipUtil.host}${requestUtil.ship}`,
            data: {logisticsCode:expressNo, orderId: id},
            header: requestUtil.header,
            method: "POST",
            success(res) {
                if (res.data.code == 0) {
                    if(state==0){
                        wx.showToast({
                            title: "发货成功",
                            icon: "success",
                            duration: 200
                        })
                        let waitShip=that.data.waitShip;
                        let ship=waitShip.splice(index, 1);
                        let alreadyShip = that.data.alreadyShip;
                        alreadyShip.push(ship);
                        that.setData({
                            waitShip: waitShip,
                            alreadyShip: alreadyShip
                        })
                    }else {
                        wx.showToast({
                            title: "修改成功",
                            icon: "success",
                            duration: 2000
                        })
                    }
                }
            }
        })
    }
});